package com.bamboo.vodservice.service.impl;

import com.bamboo.servicebase.exceptionhandler.BambooException;
import com.bamboo.vodservice.config.VideoUtil;
import com.bamboo.vodservice.service.VodService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;


/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-07-11
 */
@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadAlyVideo(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            // 管控台显示名称
            String title;
            if (StringUtils.isEmpty(originalFilename)) {
                title = "edu:" + System.currentTimeMillis();
            } else {
                title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            }
            InputStream inputStream = file.getInputStream();
            return VideoUtil.uploadStream(title, originalFilename, inputStream);
        } catch (Exception e) {
            throw new BambooException(20001, "上传视频失败");
        }

    }

    @Override
    public void deleteAlyVideo(String videoSourceId) {
        try {
            VideoUtil.deleteVideo(videoSourceId);
        } catch (Exception e) {
            throw new BambooException(20001, "删除视频失败");
        }
    }

    @Override
    public String getPlayAuth(String id) {
        try {
            return VideoUtil.getPlayAuth(id);
        } catch (Exception e) {
            throw new BambooException(20001, "获取凭证失败");
        }

    }

}
